package edu.hawaii.duedates.web;

import java.io.File;
import java.util.List;
import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebSession;
import edu.hawaii.duedates.DueDates;
import edu.hawaii.duedates.library.Library;
import edu.hawaii.duedates.book.Book;
import edu.hawaii.duedates.configuration.ConfigurationManager;

/**
 * Each user of A.com is given his own instance of StackSession that holds his personal state,
 * such as his stack trace.
 * 
 * @author Ronn Reeves.
 */
public class DueDatesSession extends WebSession {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /** The duedates for this user instance. */
  private DueDates dd;
  
  /** Holds the user id entered in the login form. */
  private String userId = "";

  /** Holds the password entered in the login form. */
  private String password = "";

  /** The error message if the stack operation failed. */
  private String errorMessage = "";
  
  /** The list of books from the query. */
  private List<Book> books;
  
  /**
   * Create a new session for this user.
   * 
   * @param application This application.
   * @param request This request.
   */
  public DueDatesSession(WebApplication application, Request request) {
    super(request);
  }

  /**
   * Set the due dates for this user instance.
   */
  public void setDueDates() {
    edu.hawaii.duedates.configuration.ConfigurationManager cm = 
      new edu.hawaii.duedates.configuration.ConfigurationManager();
    dd = cm.getDueDates(this.userId, this.password);
  }

  /**
   * Return the due dates for this user instance.
   * 
   * @return The stack.
   */
  public DueDates getDueDates() {
    return dd;
  }

   /**        
    * Set the test due dates.        
    */        
  public void setTestDueDates() {        
    File exampleDueDatesXml = new File(System.getProperty("user.dir") + "/xml/duedates.xml");
    File exampleDueDatesXsd = new File(System.getProperty("user.dir") +
      "/xml/duedates.definition.xsd");
    ConfigurationManager manager = new ConfigurationManager(exampleDueDatesXml, 
      exampleDueDatesXsd);
    dd = manager.getDueDates(this.userId, this.password);
  }   
  
  /**
   * Returns the stack error message.
   * 
   * @return The error message.
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }

  /**
   * Returns the user id entered in the login form.
   * 
   * @return The user id.
   */
  public String getUserId() {
    return this.userId;
  }

  /**
   * Returns the password entered in the login form.
   * 
   * @return The password.
   */
  public String getPassword() {
    return this.password;
  }

  /**
   * Sets the stack error message.
   * 
   * @param message The error message.
   */
  public void setErrorMessage(String message) {
    this.errorMessage = message;
  }

  
  /**
   * Sets the user id entered in the login form.
   * 
   * @param userId The user id.
   */
  public void setUserId(String userId) {
    this.userId = userId;
  }
  
  
  /**
   * Sets the password entered in the login form.
   * 
   * @param password The password.
   */
  public void setPassword(String password) {
    this.password = password;
  }
  
  
  /**
   * Sets the selected status of this library.
   * 
   * @param library The library.
   * @param selected The selected status.
   */
  public void setSelected(Library library, Boolean selected) {
    for (Library lib : this.dd.getLibraries()) {
      if (lib.equals(library)) {
        lib.setSelected(selected);
      }
    }
  }
  
  
  /**
   * Gets the selected status of the libraries.
   * 
   * @param library The library.
   * @return True if selected.
   */
  public Boolean getSelected(Library library) {
    return library.getSelected(library);
  }
  
  
  /**
   * Sets the within period to query results.
   * 
   * @param within The within period.
   */
  public void setWithin(int within) {
    this.dd.setWithin(within);
  }


  /**
   * Gets the within period to query results.
   * 
   * @return The within period.
   */
  public int getWithin() {
    return this.dd.getWithin();
  }
  
  /**
   * Sets the library to query.
   * 
   * @param library The library.
   */
  public void setLibrary(Library library) {
    this.dd.setLibrary(library);
  }


  /**
   * Gets the library being queried.
   * 
   * @return The library.
   */
  public Library getLibrary() {
    return this.dd.getLibrary();
  }

  /**
   * Sets the books from the query.
   * 
   * @param books The list of books.
   */
  public void setBooks(List<Book> books) {
    this.books = books;
  }

  /**
   * Gets the list of books from the query.
   * 
   * @return The list of books.
   */
  public List<Book> getBooks() {
    return this.books;
  }
}
